package com.laotou99.bookManage.Auth;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import com.laotou99.bookManage.IDao.UserLoginMapper;
import com.laotou99.bookManage.domain.UserLogin;


public class laotou99Realm extends AuthenticatingRealm {

	private static final Log logger = LogFactory.getLog(laotou99Realm.class);
	
	@Resource
	private UserLoginMapper userLoginMapper;
	 
	@SuppressWarnings("unused") 
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principals) 
	{
		System.out.println("doGetAuthorizationInfo");
		logger.info("doGetAuthorizationInfo->"+principals.getRealmNames());
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		return info;
	}
	
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) 
		throws AuthenticationException 
	{
		System.out.println("doGetAuthenticationInfo");
		logger.info("doGetAuthenticationInfo->"+token.toString());
		UsernamePasswordToken usernamePasswordToke = (UsernamePasswordToken)token;
		String username = usernamePasswordToke.getUsername();
		
		UserLogin userLogin = userLoginMapper.selectByUserName(username);
		
		AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(userLogin.getUsername(),
				userLogin.getPasswd(),this.getName());		
		return authcInfo;
//		return new SimpleAuthenticationInfo("aaa","123",getName());
		//return new SimpleAuthenticationInfo(new ShiroUser("aaa","123"),"aaa",ByteSource.Util.bytes("123"),getName());
	}
}
